var site = {
	hiddenClass: "hidden",
	activeClass: "active",
	tabsContainerId: "lobby-nav",
	sectionsContainerId: 'lobby-container',
	sectionSuffix: '-section',
	
	tabs: [],
	
	init: function() {
		// Check if lobby exists.
		var lobbyContainer = document.getElementById( site.tabsContainerId );
		if ( lobbyContainer ) {

			// Extract tabs from lobby markup.
			var tab = irinori.lib.getFirstChild( lobbyContainer );
			
			if ( tab )
			{
				do {
					site.tabs.push( { tabId: tab.id, sectionId: tab.id + site.sectionSuffix } )
				} while ( tab = irinori.lib.getNextSibling( tab ) );
				
				// Add onclick listener to each tab.
				for ( var i = 0; i < site.tabs.length; i++ ) {
					if ( document.getElementById( site.tabs[i].tabId ) ) {
						( function( tab ) {
							document.getElementById( tab.tabId ).onclick = function( e ) {
								site.toggleTab( tab )
							}
						} )( site.tabs[i] );
					}
				}
				
				// If there are tabs, make the first one active initially.
				// If there is another active tab, make that one active.
				if ( site.tabs.length > 0 ) {
					site.toggleTab( site.tabs[0] );
					for ( var i = 0; i < site.tabs.length; i++ ) {
						if ( irinori.lib.hasClass( document.getElementById( site.tabs[i].tabId ), "open" ) ) {
							site.toggleTab( site.tabs[i] );
						}
					}
				}
			}
		}
	},
	
	toggleTab: function( tab ) {
		for ( var i = 0; i < site.tabs.length; i++ ) {
			if ( site.tabs[i].tabId == tab.tabId )
			{
				// Make selected section visible.
				irinori.lib.removeClass( document.getElementById( site.tabs[i].sectionId ), site.hiddenClass );
				
				// Mark clicked tab as active.
				irinori.lib.addClass( document.getElementById( site.tabs[i].tabId ), site.activeClass );
			}
			else
			{
				// Hide all other sections and mark their tabs as inactive.
				irinori.lib.addClass( document.getElementById( site.tabs[i].sectionId ), site.hiddenClass )
				irinori.lib.removeClass( document.getElementById( site.tabs[i].tabId ), site.activeClass );
			}
		}
	}
};

window.onload = site.init;
